﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.IO;
using NPOI.HSSF.UserModel;
using IHISCETeim.BLL;
using IHISCETeim.Library;
using IHISCETeim.Model;
using IHISCETeim.DAL;

namespace IHISCETeim.FXTool
{
    public partial class FXAgentYuQiFrm : Form
    {
        public FXAgentYuQiFrm()
        {
            InitializeComponent();
        }
        OUserInfo objNews = null;
        private LoginState IsAuthenticated(string userName, string pwd)
        {
            try
            {
                objNews = CBO.FillObject<OUserInfo>(OUserDAL.UserLogin(userName, StringHelper.GetPwdEncrypt(pwd), ""));
                // UserId = objNews.UserId;
                if (objNews == null)
                {
                    return LoginState.inputerror;
                }
                else if (objNews.State == 0)
                {
                    return LoginState.other;
                }
                else
                {
                    return LoginState.success;
                }
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message);
                // UserId = 0;
                return LoginState.other;
            }
            finally
            {

            }
        }
        private void btnLogin_Click(object sender, EventArgs e)
        {
            try
            {               
                string username = this.txtUserName.Text;
                string pwd = this.txtPwd.Text;
                LoginState ls = IsAuthenticated(username, pwd);
                if (ls != LoginState.success)
                {
                    MessageBox.Show("登陆失败，请确认你的用户密码");
                    return;
                }
                //拉数据
                short YearTag=Convert.ToInt16( this.cbYear.Text);
                DataTable dt = FXAgentYuQiController.GetList(FXAgentYuQiExpiryType.经销商明细.ToString(), YearTag);
                DataTable dtte = dt.Copy();
                dtte.TableName = "dt0";
                this.dataSet1.Tables.Add(dtte);

                DataTable dt2 = FXAgentYuQiController.GetList(FXAgentYuQiExpiryType.年度汇总.ToString(), YearTag);
                dtte = dt2.Copy();
                dtte.TableName = "dt1";
                this.dataSet1.Tables.Add(dtte);

                this.gvAgentDetail.DataSource = this.dataSet1.Tables[0];
                this.gvYear.DataSource = this.dataSet1.Tables[1];
                //
                this.btnLogin.Enabled = false;
                this.cbYear.Enabled = false;
                this.btnSave.Enabled = true;
                MessageBox.Show("数据已加载完成");
            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message, "出错提示");
            }
        }

        private void FXAgentYuQiFrm_Load(object sender, EventArgs e)
        {
            for(int i=2011;i<=DateTime.Now.Year;i++)
            {
                this.cbYear.Items.Add(i);
            }
            this.cbYear.SelectedIndex = this.cbYear.Items.Count - 1;
           // 
        }

        private void button2_Click(object sender, EventArgs e)
        {
            btnSave.Enabled = false;

            //导入开始 
            //1.删除以前所有，根据年份
            short YearTag = Convert.ToInt16(this.cbYear.Text);
            bool x= FXAgentYuQiController.Delete(YearTag);
            this.txtImportResult.Text = "";
            //2.添加明细
            this.txtImportResult.Text = "---开始保存经销商逾期明细--" + "\r\n";
            DataTable importdt = this.dataSet1.Tables[0];
            FXAgentYuQiInfo obj = null; ;
            foreach (DataRow dr in importdt.Rows)
            {
                obj = new FXAgentYuQiInfo();
                obj.ExpiryType = FXAgentYuQiExpiryType.经销商明细.ToString();
                obj.YearTag = Convert.ToInt16( dr["年份"].ToString());
                obj.CardName = dr["经销商全称"].ToString();
                obj.AliasName = dr["简称"].ToString();
                obj.Jan = GetVal(dr["1月份"].ToString());
                obj.Feb = GetVal(dr["2月份"].ToString());
                obj.Mar = GetVal(dr["3月份"].ToString());
                obj.Apr = GetVal(dr["4月份"].ToString());
                obj.May = GetVal(dr["5月份"].ToString());
                obj.Jun = GetVal(dr["6月份"].ToString());
                obj.Jul = GetVal(dr["7月份"].ToString());
                obj.Aug = GetVal(dr["8月份"].ToString());
                obj.Sep = GetVal(dr["9月份"].ToString());
                obj.Oct = GetVal(dr["10月份"].ToString());
                obj.Nov = GetVal(dr["11月份"].ToString());
                obj.Dec = GetVal(dr["12月份"].ToString());

                string id = FXAgentYuQiController.Add(obj);
                if (!string.IsNullOrEmpty(id))
                    this.txtImportResult.Text += id + "\r\n";
            }
            this.txtImportResult.Text += "---开始保存经销商逾期明细--" + "\r\n";
            importdt = this.dataSet1.Tables[1];
            foreach (DataRow dr in importdt.Rows)
            {
                obj = new FXAgentYuQiInfo();
                obj.ExpiryType = FXAgentYuQiExpiryType.年度汇总.ToString();
                obj.YearTag = Convert.ToInt16(dr["年份"].ToString());
                obj.CardName = dr["经销商全称"].ToString();
               // obj.AliasName = dr["简称"].ToString();
                obj.Jan = GetVal(dr["1月份"].ToString());
                obj.Feb = GetVal(dr["2月份"].ToString());
                obj.Mar = GetVal(dr["3月份"].ToString());
                obj.Apr = GetVal(dr["4月份"].ToString());
                obj.May = GetVal(dr["5月份"].ToString());
                obj.Jun = GetVal(dr["6月份"].ToString());
                obj.Jul = GetVal(dr["7月份"].ToString());
                obj.Aug = GetVal(dr["8月份"].ToString());
                obj.Sep = GetVal(dr["9月份"].ToString());
                obj.Oct = GetVal(dr["10月份"].ToString());
                obj.Nov = GetVal(dr["11月份"].ToString());
                obj.Dec = GetVal(dr["12月份"].ToString());

                string id = FXAgentYuQiController.Add(obj);
                if (!string.IsNullOrEmpty(id))
                    this.txtImportResult.Text += x + "\r\n";
            }

            MessageBox.Show("完成保存");
            btnSave.Enabled = true;
        }

        private Decimal? GetVal(object v)
        {
            try
            {
                return Convert.ToDecimal(v);
            }
            catch
            {
                return null;
            }
        }
    }
}
